package com.tkjt.pms.modules.kpiadmin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.modules.common.service.PMSCommonService;
import com.tkjt.pms.modules.common.service.TkIndexValueService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tkjt.pms.calc.KpiCalculator;
import com.tkjt.pms.calc.common.CalcResult;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.vos.TkIndexValueForScoreVO;
import com.tkjt.pms.modules.common.mapper.PMSCommonMapper;
import com.tkjt.pms.modules.common.service.TkDataConfigService;
import com.tkjt.pms.modules.gm.service.TkGmService;
import com.tkjt.pms.modules.gm.vo.TkGmScoresVo;
import com.tkjt.pms.modules.kpiadmin.mapper.DataGridMapper;
import com.tkjt.pms.modules.kpiadmin.service.KpiAdminService;
import com.tkjt.pms.modules.kpiadmin.vo.DatagridVO;
import com.tkjt.pms.modules.kpiadmin.vo.HeadConditionVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/pms/kpiadmin")
public class KpiAdminController {
    @Autowired
    KpiAdminService kpiAdminService;
    @Autowired
    PMSCommonMapper commonMapper;
    @Autowired
    DataGridMapper dataGridMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private TkGmService tkGmService;
    @Autowired
    private KpiCalculator kpiCalculator;
    @Autowired
    private TkDataConfigService tkDataConfigService;
    @Autowired
    private PMSCommonService commonService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TkIndexValueService tkIndexValueService;
    /**
     * 绩效管理 -- 进度-概览
     * @param request
     * @return
     */
    @RequestMapping("kaProgressView")
    public Result<?> getKaiProgressView(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return Result.ok(kpiAdminService.getKaiProgressView(loginUser,request));
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 绩效管理 -- 进度-明细
     * @param request
     * @return
     */
    @RequestMapping("kaProgressDetail")
    public Result<?> getKaiProgressDetail(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return Result.ok(kpiAdminService.getKaiProgressDetail(loginUser,request));
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }






    /**
     * 设定目标值-列表条件
     * 表头信息
     * @return
     */
    @RequestMapping("kaTargetConditions")
    public Result<?> kaTargetConditions(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        List<Map<String,Object>> indexPeriods = commonMapper.getDictItemByDictCode("yearMonth");
        HeadConditionVO headConditionVO = new HeadConditionVO();
        headConditionVO.setIndexPeriods(indexPeriods);
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> codeMap = new HashMap<>();
        codeMap.put("name","指标名称");
        codeMap.put("code","indexName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标编码");
        codeMap.put("code","indexCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值名称");
        codeMap.put("code","indexValueName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值编码");
        codeMap.put("code","indexValueCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        try {
            List<Map<String,Object>> indexGroups = kpiAdminService.indexGroupConditions(loginUser,tabHeader,null,datagridVO.getIndexYearMonth());
            headConditionVO.setIndexGroup(indexGroups);
            return Result.ok(indexGroups);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 设定目标值-列表信息
     * @return
     */
    @RequestMapping("kaTargetDatagrid")
    public Result<?> kaTargetDatagrid(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return kpiAdminService.kaTargetDatagrid(loginUser,datagridVO,null);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 设定目标值-考核指标
     * @return
     */
    @GetMapping("indexCodeList")
    public Result<?> indexCodeList(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return Result.ok(kpiAdminService.getIndexCodeByTarget(loginUser,request.getParameter("indexGroup")));
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }
    /**
     * 设定目标值-列表保存
     * @return
     */
    @RequestMapping("kaTargetSave")
    public Result<?> kaTargetSave(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return kpiAdminService.indexValueDataSave(loginUser,datagridVO);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }


    /**
     * 目标值实际值填报/目标值实际值导入-列表条件
     * 表头信息
     * @return
     */
    @RequestMapping("kaFillConditions")
    public Result<?> kaFillConditions(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        List<Map<String,Object>> indexPeriods = commonMapper.getDictItemByDictCode("yearMonth");
        HeadConditionVO headConditionVO = new HeadConditionVO();
        headConditionVO.setIndexPeriods(indexPeriods);
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> codeMap = new HashMap<>();
        codeMap.put("name","指标名称");
        codeMap.put("code","indexName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标编码");
        codeMap.put("code","indexCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值名称");
        codeMap.put("code","indexValueName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值编码");
        codeMap.put("code","indexValueCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        try {
            List<Map<String,Object>> indexGroups = kpiAdminService.indexGroupConditions(loginUser,tabHeader,"noVirtual",datagridVO.getIndexYearMonth());
            headConditionVO.setIndexGroup(indexGroups);
            return Result.ok(indexGroups);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 绩效管理员绩效审核详情/审核员绩效考核详情-列表条件
     * 表头信息
     * @return
     */
    @RequestMapping("kaFillConditionsForChangeUser")
    public Result<?> kaFillConditionsForChangeUser(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        List<Map<String,Object>> indexPeriods = commonMapper.getDictItemByDictCode("yearMonth");
        HeadConditionVO headConditionVO = new HeadConditionVO();
        headConditionVO.setIndexPeriods(indexPeriods);
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> codeMap = new HashMap<>();
        codeMap.put("name","指标名称");
        codeMap.put("code","indexName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标编码");
        codeMap.put("code","indexCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值名称");
        codeMap.put("code","indexValueName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值编码");
        codeMap.put("code","indexValueCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        try {
            List<Map<String,Object>> indexGroups = kpiAdminService.indexGroupConditionsForChangeUser(loginUser,tabHeader,"noVirtual",datagridVO.getIndexYearMonth());
            headConditionVO.setIndexGroup(indexGroups);
            return Result.ok(indexGroups);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 绩效填报-列表信息
     * @return
     */
    @RequestMapping("kaFillDatagrid")
    public Result<?> kaFillDatagrid(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        datagridVO.setIndexValueEdit("1");
        try {
            return kpiAdminService.kaFillDatagrid(loginUser,datagridVO,"noVirtual");
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 目标值实际值填报-列表保存
     * @return
     */
    @RequestMapping("kaFillSave")
    public Result<?> kaFillSave(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return kpiAdminService.kaFillSave(loginUser,datagridVO);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 权重管理-列表条件
     * 表头信息
     * @return
     */
    @RequestMapping("kaWeightConditions")
    public Result<?> kaWeightConditions(){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        List<Map<String,Object>> indexPeriods = commonMapper.getDictItemByDictCode("yearMonth");
        HeadConditionVO headConditionVO = new HeadConditionVO();
        headConditionVO.setIndexPeriods(indexPeriods);
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> codeMap = new HashMap<>();
        codeMap.put("name","指标名称");
        codeMap.put("code","indexName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标编码");
        codeMap.put("code","indexCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","权重格式标识");
        codeMap.put("code","indexType");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","权重格式");
        codeMap.put("code","indexTypeName");
        tabHeader.add(codeMap);
        try {
            List<Map<String,Object>> indexGroups = kpiAdminService.weightConditions(loginUser,tabHeader,"noVirtual");
            headConditionVO.setIndexGroup(indexGroups);
            return Result.ok(indexGroups);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 权重管理-列表信息
     * @return
     */
    @RequestMapping("kaWeightDatagrid")
    public Result<?> kaWeightDatagrid(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return kpiAdminService.kaWeightDatagrid(loginUser,datagridVO,"noVirtual");
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 权重管理-列表保存
     * @return
     */
    @RequestMapping("kaWeightSave")
    public Result<?> kaWeightSave(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return kpiAdminService.kaWeightSave(loginUser,datagridVO);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }


    /**
     **绩效汇总评分-评分列表（绩效管理员评分）
     * @param
     * @return
     */
    @RequestMapping(value = "kaGradeDatagrid")
    public Result<?> kaGradeDatagrid(@RequestBody TkIndexValueForScoreVO tkIndexValue){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        List<String> userRoleCodes = sysUserService.getRole(loginUser.getUsername());
        if(!userRoleCodes.contains("ykkpiowner") && !userRoleCodes.contains("ykgm")) {
            return Result.error("用户["+loginUser.getUsername()+"]无权限查看数据！");
        }
        try {
            return kpiAdminService.kaGradeDatagrid(loginUser,tkIndexValue,tkIndexValue.getPageNo(),tkIndexValue.getPageSize());
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }
    /**
     **双向评分记录列表(从绩效管理员评分页面跳转)
     * @param
     * @return
             */
    @RequestMapping(value = "kaCgDatagrid")
    public Result<?> cgGmDatagrid(@RequestBody TkIndexValueForScoreVO tkIndexValue,HttpServletRequest req){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        String indexDepart = tkIndexValue.getIndexDepart();
        if(indexDepart == null || "".equals(indexDepart)){
            return Result.error("参数缺失");
        }
        String indexScorer = tkIndexValue.getIndexScorer();
        if(indexScorer == null || "".equals(indexScorer)){
            return Result.error("参数缺失");
        }
        String indexValueCode = tkIndexValue.getIndexValueCode();
        if(indexValueCode == null || "".equals(indexValueCode)){
            return Result.error("参数缺失");
        }
        try {
            SysUser user = sysUserService.getUserByName(indexScorer);
            QueryWrapper<SysDepart> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.lambda().eq(SysDepart::getOrgCode,tkIndexValue.getIndexDepart());
            SysDepart sysDepart = sysDepartService.getOne(objectQueryWrapper);
            Map<String,Object> pageMap = tkGmService.cgDatagrid(loginUser,tkIndexValue,tkIndexValue.getPageNo(),tkIndexValue.getPageSize());
            pageMap.put("indexYearMonth",tkIndexValue.getIndexYearMonth());
            pageMap.put("indexDepartName",sysDepart != null ? sysDepart.getDepartName() : null);
            pageMap.put("indexDepart",tkIndexValue.getIndexDepart());
            pageMap.put("indexScorerName",user != null ? user.getRealname() : null);
            pageMap.put("indexScorer",tkIndexValue.getIndexScorer());
            return Result.ok(pageMap);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     ** 绩效得分记录(从绩效管理员评分页面跳转)
     * @param
     * @return
     */
    @RequestMapping(value = "kaScoreDatagrid")
    public Result<?> scoreGmDatagrid(@RequestBody TkIndexValueForScoreVO tkIndexValue,HttpServletRequest req){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return tkGmService.scoreGmDatagrid(loginUser,tkIndexValue);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     ** 绩效汇总评分-评分保存（绩效管理员评分）
     * @param
     * @return
     */
    @RequestMapping(value = "kaGradeSave", method = RequestMethod.POST)
    public Result<?> kaGradeSave(@RequestBody TkGmScoresVo tkGmScoresVo){

        List<TkIndexValueForScoreVO> vos = tkGmScoresVo.getTkIndexValueVOs();
        if(CollectionUtils.isEmpty(vos)) {
            return Result.ok();
        }

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        String yearMonth = tkGmScoresVo.getIndexYearMonth();
        if(StringUtils.isEmpty(yearMonth)) {
            yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        }

        //保存总经理评分和评分意见
        Result<?> result = tkGmService.saveScores(loginUser, yearMonth, vos);
        String redisKey = loginUser.getUsername()+"@"+loginUser.getOrgCode()+"@"+yearMonth;
        @SuppressWarnings("unchecked")
		List<Map<String,Object>> saveDataList = (List<Map<String,Object>>)redisUtil.get(redisKey);
        if(null == saveDataList) {
        	saveDataList = new ArrayList<>();
        }
        //redisUtil.del(redisKey);
        //List<Map<String,Object>> saveDataList = new ArrayList<>();
        for(TkIndexValueForScoreVO indexValueForScoreVO : vos){
            QueryWrapper<TkIndexValue> indexValueQuery = new QueryWrapper<>();
            indexValueQuery.lambda().eq(TkIndexValue::getId,indexValueForScoreVO.getId());
            TkIndexValue tkIndexValue = tkIndexValueService.getOne(indexValueQuery);
            String indexValueCode = tkIndexValue.getIndexValueCode();
            String indexDepart = tkIndexValue.getIndexDepart();
            String indexGroup = "A01A01A01".equals(indexDepart.substring(0,9)) ? "C" : "G";
            Map<String,Object> saveData = new HashMap<>();
            saveData.put("indexValueCode",indexValueCode);
            saveData.put("indexDepart",indexDepart);
            saveData.put("indexGroup",indexGroup);
            saveDataList.add(saveData);
        }
        redisUtil.set(redisKey,saveDataList);
        return result;
    }

    /**
     ** 绩效汇总评分-评分计算（绩效管理员评分）
     * @param
     * @return
     */
    @RequestMapping(value = "kaGradeCalc", method = RequestMethod.POST)
    public Result<?> kaGradeCalc(@RequestBody TkGmScoresVo tkGmScoresVo){

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        String yearMonth = tkGmScoresVo.getIndexYearMonth();
        if(StringUtils.isEmpty(yearMonth)) {
            yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        }

        //保存总经理评分和评分意见
        try {
            List<TkIndexValueForScoreVO> vos = tkGmScoresVo.getTkIndexValueVOs();
            if(!CollectionUtils.isEmpty(vos)) {
                Result<?> result = tkGmService.saveScores(loginUser, yearMonth, vos);
                if(result.getCode() != 200){
                    return result;
                }
                String redisKey = loginUser.getUsername()+"@"+loginUser.getOrgCode()+"@"+yearMonth;
                @SuppressWarnings("unchecked")
        		List<Map<String,Object>> saveDataList = (List<Map<String,Object>>)redisUtil.get(redisKey);
                if(null == saveDataList) {
                	saveDataList = new ArrayList<>();
                }
                for(TkIndexValueForScoreVO indexValueForScoreVO : vos){
                    QueryWrapper<TkIndexValue> indexValueQuery = new QueryWrapper<>();
                    indexValueQuery.lambda().eq(TkIndexValue::getId,indexValueForScoreVO.getId());
                    TkIndexValue tkIndexValue = tkIndexValueService.getOne(indexValueQuery);
                    String indexValueCode = tkIndexValue.getIndexValueCode();
                    String indexDepart = tkIndexValue.getIndexDepart();
                    String indexGroup = "A01A01A01".equals(indexDepart.substring(0,9)) ? "C" : "G";
                    Map<String,Object> saveData = new HashMap<>();
                    saveData.put("indexValueCode",indexValueCode);
                    saveData.put("indexDepart",indexDepart);
                    saveData.put("indexGroup",indexGroup);
                    saveDataList.add(saveData);
                }
                redisUtil.set(redisKey,saveDataList); 
            }
        }catch (Exception e){
        	log.info(e.getMessage(), e);
            return Result.error("保存数据异常");
        }
        try {
            log.info("kaGradeCalc, start....");
            //计算绩效分数
            String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
            String indexYearMonth = tkGmScoresVo.getIndexYearMonth();
            String redisKey = loginUser.getUsername()+"@"+loginUser.getOrgCode()+"@"+yearMonth;
            @SuppressWarnings("unchecked")
            List<Map<String,Object>> saveDataList = (List<Map<String,Object>>)redisUtil.get(redisKey);
            if(saveDataList != null && saveDataList.size() > 0){
                for(Map<String,Object> saveData : saveDataList) {
                    String indexValueCode = saveData.get("indexValueCode").toString();
                    String indexDepart = saveData.get("indexDepart").toString();
                    String indexGroup = saveData.get("indexGroup").toString();
                    CalcResult<String> calcResult = kpiCalculator.doCalculatorDirectly(dataType, indexYearMonth, indexGroup, indexValueCode, indexDepart);
                    if(!calcResult.isSuccess()) {
                        return Result.error(calcResult.getMessage());
                    }
                }
                redisUtil.set(redisKey, new ArrayList<Map<String,Object>>());
            }
            log.info("kaGradeCalc, end.");
            return Result.ok();
        }catch (Exception e){
        	log.info(e.getMessage(), e);
            return Result.error("得分计算异常！");
        }
    }

    /**
     ** 绩效汇总计算-列表条件
     ** 表头信息
     * @return
     */
    @RequestMapping("kaMgrConditions")
    public Result<?> kaMgrConditions(@RequestBody TkGmScoresVo tkGmScoresVo){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        List<Map<String,Object>> indexPeriods = commonMapper.getDictItemByDictCode("yearMonth");
        HeadConditionVO headConditionVO = new HeadConditionVO();
        headConditionVO.setIndexPeriods(indexPeriods);
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> codeMap = new HashMap<>();
        codeMap.put("name","指标名称");
        codeMap.put("code","indexName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标编码");
        codeMap.put("code","indexCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标分项名称");
        codeMap.put("code","indexValueName");
        tabHeader.add(codeMap);
        codeMap = new HashMap<>();
        codeMap.put("name","指标值编码");
        codeMap.put("code","indexValueCode");
        codeMap.put("isShow",false);
        tabHeader.add(codeMap);
        try {
            List<Map<String,Object>> indexGroups = kpiAdminService.indexGroupConditionsForChangeUser(loginUser,tabHeader,"noVirtual",tkGmScoresVo.getIndexYearMonth());
            headConditionVO.setIndexGroup(indexGroups);
            return Result.ok(indexGroups);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 绩效汇总计算-列表信息
     * @return
     */
    @RequestMapping("kaMgrDatagrid")
    public Result<?> kaMgrDatagrid(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        //1:考核;2:权重;3:加减分项
        //String[] indexTypes = {"1","2","3"};
       // datagridVO.setIndexTypes(Arrays.asList(indexTypes));
        try {
            return kpiAdminService.kaMgrDatagrid(loginUser,datagridVO,"noVirtual");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 审核员绩效考核详情/绩效管理员绩效审核详情  列表信息
     * @return
     */
    @RequestMapping("auditDetailDatagrid")
    public Result<?> auditDetailDatagrid(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        //1:考核;2:权重;3:加减分项
        //String[] indexTypes = {"1","2","3"};
        // datagridVO.setIndexTypes(Arrays.asList(indexTypes));
        try {
            return kpiAdminService.auditDetailDatagrid(loginUser,datagridVO,"noVirtual");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * 绩效汇总计算-列表保存
     * @return
     */
    @RequestMapping("kaMgrSave")
    public Result<?> kaMgrSave(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
            return kpiAdminService.indexValueDataSave(loginUser,datagridVO);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }
    
    /**
     * 绩效汇总计算-执行计算
     * @return
     */
    @RequestMapping("kaMgrCalc")
    public Result<?> kaMgrCalc(@RequestBody DatagridVO datagridVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
        	kpiAdminService.indexValueDataSave(loginUser,datagridVO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("保存数据异常！");
        }
        try {
        	Set<String> indexGroups = new HashSet<String>();
        	indexGroups.add("C");
        	indexGroups.add("G");
            String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
            String indexYearMonth = datagridVO.getIndexYearMonth();
            if(indexYearMonth.length() > 6){
                indexYearMonth = indexYearMonth.substring(0,4)+indexYearMonth.substring(6);
            }
            CalcResult<String> calcResult = kpiCalculator.doCalculator(dataType, indexYearMonth, indexGroups);
            if(!calcResult.isSuccess()) {
            	return Result.error(calcResult.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("计算启动异常");
        }
        return Result.ok("得分计算任务启动");
    }

    /**
     * 绩效汇总计算-审核状态信息
     * @return
     */
    @RequestMapping("kaMgrStatus")
    public Result<?> kaMgrStatus(@RequestBody TkIndexValueForScoreVO indexValueForScoreVO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        try {
        	String indexYearMonth = indexValueForScoreVO.getIndexYearMonth();
        	//处理累计月份
        	if(indexYearMonth!=null && indexYearMonth.length()>=8) {
        		indexYearMonth = indexYearMonth.trim();
        		indexYearMonth = indexYearMonth.substring(0,4) + indexYearMonth.substring(6);
        		indexValueForScoreVO.setIndexYearMonth(indexYearMonth);
        	}
            return kpiAdminService.kaMgrAudit(loginUser,indexValueForScoreVO);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 所有镜像汇总评分年月信息
     * @return
     */
    @RequestMapping(value = "yearMonthList")
    public Result<?> yearMonthList(){
        try {
            List<Map<String, Object>> yearMonthList = commonService.yearMonthList();
            List<Map<String, Object>> mtotalYearMonthList = commonService.allMTotalYearMonthList();
            yearMonthList.addAll(mtotalYearMonthList);
            return Result.ok(yearMonthList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 当前考核期汇总评分年月信息
     * @return
     */
    @RequestMapping(value = "currentYearMonthList")
    public Result<?> currentYearMonthList(){
        try {
            List<Map<String, Object>> yearMonthList = commonService.yearMonthList();
            List<Map<String, Object>> mtotalYearMonthList = commonService.currentMTotalYearMonthList();
            yearMonthList.addAll(mtotalYearMonthList);
            return Result.ok(yearMonthList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

}
